
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:gold/model/address_model.dart';
import 'package:provider/provider.dart';
import 'package:gold/base/provider_scaffold.dart';
import 'package:gold/ui/my/address/new_address_viewmodel.dart';
import 'package:gold/utils/xfg_color.dart';
import 'package:gold/utils/xfg_size.dart';
import 'package:gold/widgets/common_widgets.dart';
import 'package:gold/widgets/xfg_text.dart';

/// @description: 地址页面
/// @author tangzhentao
/// @created at 21:21 2024/12/7

class NewAddressPage extends StatelessWidget {
  /// 地址id
  final AddressModel? address;

  const NewAddressPage({super.key, this.address});

  @override
  Widget build(BuildContext context) {
    return ProviderScaffold<NewAddressViewmodel>(
        resizeToAvoidBottomInset: false,
        appBar: (ctx) => xfgAppbar(title: '新增收货地址'),
        model: NewAddressViewmodel(address),
        builder: (ctx, address) => SingleChildScrollView(
          padding: EdgeInsets.all(XfgSize().size10),
          child: Column(
            children: [
              Container(
                decoration: white6Decoration,
                child: Column(
                  children: [
                    GestureDetector(
                      onTap: address.pickAddress,
                      child: Container(
                        padding: EdgeInsets.symmetric(horizontal: XfgSize().size16, vertical: XfgSize().size10),
                        constraints: BoxConstraints(minHeight: XfgSize().size56),
                        child: Row(
                          children: [
                            Container(
                              width: XfgSize().size80,
                              alignment: Alignment.centerLeft,
                              child: XfgText('地址', fontSize: XfgSize().text18, color: XfgColor.text1, bold: FontWeight.bold),
                            ),
                            Expanded(
                              child: TextField(
                                controller: address.addressController,
                                focusNode: address.addressNode,
                                maxLines: null,
                                style: TextStyle(fontSize: XfgSize().text18, color: XfgColor.text2),
                                decoration: InputDecoration(
                                    border: InputBorder.none,
                                    hintText: '请输入详细地址',
                                    hintStyle: TextStyle(fontSize: XfgSize().text18, color: XfgColor.text3)
                                ),
                              ),
                            ),
                          ],
                        ),
                      ),
                    ),
                    _strokeLine,
                    Container(
                      padding: EdgeInsets.symmetric(horizontal: XfgSize().size16, vertical: XfgSize().size10),
                      constraints: BoxConstraints(minHeight: XfgSize().size56),
                      child: Row(
                        children: [
                          Container(
                            width: XfgSize().size80,
                            alignment: Alignment.centerLeft,
                            child: XfgText('门牌号', fontSize: XfgSize().text18, color: XfgColor.text1, bold: FontWeight.bold),
                          ),
                          Expanded(
                            child: TextField(
                              controller: address.infoController,
                              focusNode: address.infoNode,
                              style: TextStyle(fontSize: XfgSize().text18, color: XfgColor.text2),
                              decoration: InputDecoration(
                                border: InputBorder.none,
                                hintText: '例：15栋3层',
                                hintStyle: TextStyle(fontSize: XfgSize().text18, color: XfgColor.text3)
                              ),
                            ),
                          )
                        ],
                      ),
                    ),
                    _strokeLine,
                    Container(
                      padding: EdgeInsets.symmetric(horizontal: XfgSize().size16, vertical: XfgSize().size10),
                      constraints: BoxConstraints(minHeight: XfgSize().size56),
                      child: Column(
                        children: [
                          Row(
                            children: [
                              Container(
                                width: XfgSize().size80,
                                alignment: Alignment.centerLeft,
                                child: XfgText('收货人', fontSize: XfgSize().text18, color: XfgColor.text1, bold: FontWeight.bold),
                              ),
                              Expanded(
                                child: TextField(
                                  controller: address.nameController,
                                  focusNode: address.nameNode,
                                  style: TextStyle(fontSize: XfgSize().text18, color: XfgColor.text2),
                                  decoration: InputDecoration(
                                      border: InputBorder.none,
                                      hintText: '收货人姓名',
                                      hintStyle: TextStyle(fontSize: XfgSize().text18, color: XfgColor.text3)
                                  ),
                                ),
                              )
                            ],
                          ),
                          Builder(builder: (ctx) {
                            final gender = ctx.select<NewAddressViewmodel, String?>((k) => k.sex);
                            return Container(
                              margin: EdgeInsets.only(top: XfgSize().size18),
                              padding: EdgeInsets.only(left: XfgSize().size80),
                              child: Row(
                                children: [
                                  _genderWidget('男士', gender == 'M', onTap: () => address.sex = 'M'),
                                  SizedBox(width: XfgSize().size52),
                                  _genderWidget('女士', gender == "F", onTap: () => address.sex = "F")
                                ],
                              ),
                            );
                          })
                        ],
                      ),
                    ),
                    _strokeLine,
                    Container(
                      padding: EdgeInsets.symmetric(horizontal: XfgSize().size16, vertical: XfgSize().size10),
                      constraints: BoxConstraints(minHeight: XfgSize().size56),
                      child: Row(
                        children: [
                          Container(
                            width: XfgSize().size80,
                            alignment: Alignment.centerLeft,
                            child: XfgText('手机号', fontSize: XfgSize().text18, color: XfgColor.text1, bold: FontWeight.bold),
                          ),
                          Expanded(
                            child: TextField(
                              controller: address.phoneController,
                              focusNode: address.phoneNode,
                              keyboardType: TextInputType.phone,
                              style: TextStyle(fontSize: XfgSize().text18, color: XfgColor.text2),
                              decoration: InputDecoration(
                                  border: InputBorder.none,
                                  hintText: '收货人手机号',
                                  hintStyle: TextStyle(fontSize: XfgSize().text18, color: XfgColor.text3)
                              ),
                            ),
                          )
                        ],
                      ),
                    ),
                  ],
                ),
              ),
              /// 是否默认地址
              Container(
                decoration: white6Decoration,
                margin: EdgeInsets.only(top: XfgSize().size10, bottom: XfgSize().size116),
                padding: EdgeInsets.symmetric(horizontal: XfgSize().size16, vertical: XfgSize().size24),
                child: Row(
                  children: [
                    Expanded(
                      child: XfgText('设为默认地址\n', fontSize: XfgSize().text18, color: XfgColor.text1, bold: FontWeight.bold, height: 1.5, children: [
                        TextSpan(text: '启动时将优先定位在默认地址，避免选错', style: TextStyle(fontSize: XfgSize().text14, color: XfgColor.text3, fontWeight: FontWeight.normal))
                      ]),
                    ),
                    CupertinoSwitch(
                        activeTrackColor: XfgColor.theme,
                        value: ctx.select<NewAddressViewmodel, int?>((k) => k.isDefault) == 1,
                        onChanged: (value) => address.isDefault = value ? 1:0
                    )
                  ],
                ),
              ),
              TextButton(
                onPressed: () => address.save(),
                style: xfgButtonStyle(Size(XfgSize().size342, XfgSize().size50), radius: XfgSize().size50),
                child: XfgText('保存', fontSize: XfgSize().text18, color: XfgColor.white, bold: FontWeight.bold)
              )
            ],
          ),
        )
    );
  }

  /// 分割线
   Widget get _strokeLine => Container(color: XfgColor.colorE, height: XfgSize().size0_5);

  /// 性别Widget
  Widget _genderWidget(String text, bool select, {GestureTapCallback? onTap}) => InkWell(
    onTap: onTap,
    child: Row(
      mainAxisSize: MainAxisSize.min,
      children: [
        xfgRadio(value: select, size: XfgSize().size18),
        SizedBox(width: XfgSize().size14),
        XfgText(text, fontSize: XfgSize().text18, color: XfgColor.text3)
      ],
    ),
  );
}
